package com.example.simple_file_chooser;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import android.view.View;
import android.widget.Toast;


import java.util.Arrays;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class PermissionActivity extends AppCompatActivity implements View.OnClickListener {

    private Context mContext;
    // 相机权限、多个权限
    private final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    private final String[] PERMISSIONS = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE
            ,  Manifest.permission.READ_PHONE_STATE,Manifest.permission.CAMERA};

    // 打开相机请求Code，多个权限请求Code
    private final int REQUEST_CODE_CAMERA = 1,REQUEST_CODE_PERMISSIONS=2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;


//        setContentView(R.layout.activity_main_live);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {

                }else {
                    Toast.makeText(this,"ACTION_MANAGE_OVERLAY_PERMISSION权限已被拒绝",Toast.LENGTH_SHORT).show();;
                }
            }

        }
    }

    // 普通申请一个权限
    private void requestPermission(){
        new PermissionUtil().checkAndRequestPermission(mContext, PERMISSION_CAMERA, 0,
                new PermissionUtil.PermissionCheckCallBack() {
                    @Override
                    public void ungranted() {

                    }

                    @Override
            public void onHasPermission() {
                // 权限已被授予
                hasPermission();
            }

                    @Override
                    public void onUserHasAlreadyTurnedDown(String... permission) {

                    }

                    @Override
                    public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {

                    }
                });
    }

    // 自定义申请一个权限
    private void requestPermission1(){
        new PermissionUtil().checkAndRequestMorePermissions(mContext, PERMISSIONS,
                0,new PermissionUtil.PermissionCheckCallBack() {
                    @Override
                    public void ungranted() {

                    }

                    @Override
            public void onHasPermission() {
            }

            @Override
            public void onUserHasAlreadyTurnedDown(String... permission) {
                showExplainDialog(permission, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        new PermissionUtil().requestPermission(mContext, PERMISSION_CAMERA, REQUEST_CODE_CAMERA);
                    }
                });
            }

            @Override
            public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                new PermissionUtil().requestPermission(mContext, PERMISSION_CAMERA, REQUEST_CODE_CAMERA);
            }
        });
    }

    // 普通申请多个权限
    private void requestMorePermissions(){
        new PermissionUtil().checkAndRequestMorePermissions(mContext, PERMISSIONS, REQUEST_CODE_PERMISSIONS,
                new PermissionUtil.PermissionCheckCallBack() {
                    @Override
                    public void ungranted() {

                    }

                    @Override
            public void onHasPermission() {
                // 权限已被授予
//                toCamera();

            }

                    @Override
                    public void onUserHasAlreadyTurnedDown(String... permission) {

                    }

                    @Override
                    public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {

                    }
                });
    }

    public void hasPermission(){

    }
    // 自定义申请多个权限
    private void requestMorePermissions1(){
        new PermissionUtil().checkMorePermissions(mContext, PERMISSIONS, new PermissionUtil.PermissionCheckCallBack() {
            @Override
            public void ungranted() {

            }

            @Override
            public void onHasPermission() {
                hasPermission();
            }

            @Override
            public void onUserHasAlreadyTurnedDown(String... permission) {
                showExplainDialog(permission, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        new PermissionUtil().requestMorePermissions(mContext, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
                    }
                });
            }

            @Override
            public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                new PermissionUtil().requestMorePermissions(mContext, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
            }
        });
    }

    private void toCamera() {
        Intent intent = new Intent();
        intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
        startActivity(intent);
    }



    /**
     * 解释权限的dialog
     *
     */
    private void showExplainDialog(String[] permission, DialogInterface.OnClickListener onClickListener) {
        new AlertDialog.Builder(mContext)
                .setTitle("申请权限")
                .setMessage("我们需要" + Arrays.toString(permission)+"权限")
                .setPositiveButton("确定", onClickListener)
                .show();
    }

    /**
     * 显示前往应用设置Dialog
     *
     */
    private void showToAppSettingDialog() {
        new AlertDialog.Builder(mContext)
                .setTitle("需要权限")
                .setMessage("我们需要相关权限，才能实现功能，点击前往，将转到应用的设置界面，请开启应用的相关权限。")
                .setPositiveButton("前往", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        new PermissionUtil().toAppSetting(mContext);
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CODE_CAMERA:
                if (new PermissionUtil().isPermissionRequestSuccess(grantResults)) {
                    // 权限申请成功
                    toCamera();
                } else {
                    Toast.makeText(mContext, "打开相机失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case REQUEST_CODE_PERMISSIONS:
                new PermissionUtil().onRequestMorePermissionsResult(mContext, PERMISSIONS, new PermissionUtil.PermissionCheckCallBack() {
                    @Override
                    public void ungranted() {

                    }

                    @Override
                    public void onHasPermission() {
                        hasPermission();
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDown(String... permission) {
                        Toast.makeText(mContext, "我们需要" + Arrays.toString(permission) + "权限", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                        Toast.makeText(mContext, "我们需要" + Arrays.toString(permission) + "权限", Toast.LENGTH_SHORT).show();
                        showToAppSettingDialog();
                    }
                });


        }
    }


    @Override
    public void onClick(View view) {

    }
}
